//
//  HomeControlViewController.m
//  NuwaRobot
//
//  Created by Kendama on 2017/12/2.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "HomeControlViewController.h"
#import "MyTableViewCell.h"
#import "HomeTableHeaderView.h"
#import "AddHomeControlViewController.h"
#import "AirConditionerViewController.h"
#import "TVRemoteViewController.h"
#import "STBViewController.h"
#import "InfraredModel.h"
#import "GetHomeControlListApi.h"
#import "DeleteHomeControlApi.h"

@interface HomeControlViewController ()<UITableViewDelegate,UITableViewDataSource,MGSwipeTableCellDelegate>

@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) NSMutableArray *dataSource;
@property(nonatomic,strong) HomeTableHeaderView *tableHeaderView;


@end

@implementation HomeControlViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewDidAppear:(BOOL)animated
{
    [self getHomeControlList];
}

#pragma mark --- 点击事件
- (void)onRightButtonClick:(id)sender
{
    AddHomeControlViewController *vc = [AddHomeControlViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- 事件处理
- (void)getHomeControlList
{
    GetHomeControlListApi *api = [[GetHomeControlListApi alloc] initWithDic:@{@"sn":EXRACT_OBJECT(DEVICEMAC)}];
    [api dtStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        [self.dataSource removeAllObjects];
        [self.dataSource addObjectsFromArray:[NSArray yy_modelArrayWithClass:[InfraredModel class] json:request.responseJSONObject[@"data"]]];
        [self.tableView reloadData];
    } failure:nil];
}

#pragma mark --- tabelview代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return px1920Height(180);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return px1920Height(30);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyTableViewCell *cell = [MyTableViewCell dequeueReusableWithTableView:tableView];

    InfraredModel *model = self.dataSource[indexPath.section];
    cell.infraredModel = model;
    cell.rightButtons = @[[MGSwipeButton buttonWithTitle:@"删除" backgroundColor:rgba(251,74,79,1)]];
    cell.delegate = self;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    InfraredModel *model = self.dataSource[indexPath.section];
    if ([model.type isEqualToString:@"2"])
    {
        TVRemoteViewController *vc = [TVRemoteViewController new];
        vc.infraredModel = model;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([model.type isEqualToString:@"7"])
    {
        AirConditionerViewController *vc = [AirConditionerViewController new];
        vc.infraredModel = model;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else
    {
        STBViewController *vc = [STBViewController new];
        vc.infraredModel = model;
        [self.navigationController pushViewController:vc animated:YES];
    }

}

-(BOOL) swipeTableCell:(UITableViewCell *)cell tappedButtonAtIndex:(NSInteger) index direction:(MGSwipeDirection)direction fromExpansion:(BOOL) fromExpansion
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    
    if (direction == MGSwipeDirectionRightToLeft)
    {
        InfraredModel *model = self.dataSource[indexPath.section];
        switch (index) {
            case 0:
            {
                [[AlertViewManager shareManager] initWithTitle:@"提示" message:@"您确定要删除此遥控？" cancleButtonTitle:@"取消" OtherButtonsArray:@[@"确定"] clickAtIndex:^(NSInteger buttonIndex, UIAlertController *alertController) {
                    if (buttonIndex == 1)
                    {
                        DeleteHomeControlApi *api = [[DeleteHomeControlApi alloc] initWithDic:@{@"id":model.ID}];
                        [api dtStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
                            [SVProgressHUD showSuccessWithStatus:[request responseJSONObject][@"errmsg"]];
                            [self getHomeControlList];
                        } failure:nil];
                    }
                }];
            }
                break;
            
                
            default:
                break;
        }
        
    }
    
    return YES;
}

#pragma mark --- 懒加载
- (UITableView *)tableView
{
    if(!_tableView)
    {
        UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        [superView addSubview:_tableView];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableHeaderView = self.tableHeaderView;
    }
    return _tableView;
}

//- (NSArray *)dataSource
//{
//    if(!_dataSource)
//    {
//        _dataSource = @[@[@"客厅电视机",@"客厅空调",@"卧室机顶盒"],@[@"icon_TV",@"icon_airconditioner",@"icon_STB"]];
//    }
//    return _dataSource;
//}
- (NSMutableArray *)dataSource
{
    if(!_dataSource)
    {
        _dataSource = [[NSMutableArray alloc]init];
    }
    return _dataSource;
}


- (HomeTableHeaderView *)tableHeaderView
{
    if(!_tableHeaderView)
    {
        UIView *superView = self.tableView;
        _tableHeaderView = [[HomeTableHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, px1920Height(100))];
        [superView addSubview:_tableHeaderView];
    }
    return _tableHeaderView;
}


- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(64);
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(40));
        make.right.mas_equalTo(superView.mas_right).with.offset(-px1080Width(40));
        make.bottom.mas_equalTo(superView.mas_bottom);
    }];
    
    
}

- (void)setNavigationStyle
{
    [self setNavigationBarTitle:@"家居控制"];
    [self setRightButtonImage:imageWithName(@"nav_add")];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
